package com.gdust.lease.web.admin.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gdust.lease.model.entity.*;
import com.gdust.lease.web.admin.mapper.*;
import com.gdust.lease.web.admin.service.LeaseAgreementService;
import com.gdust.lease.web.admin.vo.agreement.AgreementQueryVo;
import com.gdust.lease.web.admin.vo.agreement.AgreementVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author syun
 * @description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
 * @createDate 2025-1-1 15:06:08
 */
@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {
    //注入租约信息表mapper
    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;

    //注入公寓信息表的mapper
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    //注入房间信息表的mapper
    @Autowired
    private RoomInfoMapper roomInfoMapper;

    //注入支付类型表的mapper
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;

    //注入租期信息表的mapper
    @Autowired
    private LeaseTermMapper leaseTermMapper;


    /**
     * 根据条件分页查询租约列表
     * @param page 分页对象
     * @param queryVo 查询条件
     * @return
     */
    @Override
    public IPage<AgreementVo> pageAgreementByQuery(IPage<IPage> page, AgreementQueryVo queryVo) {
        //自定义查询：根据条件分页查询租约列表
        return leaseAgreementMapper.pageAgreementByQuery(page,queryVo);
    }

    /**
     * 根据id查询租约信息
     *
     * 思路：根据传入的租约id，去查询租约信息表以及相关联的公寓信息表、房间信息表、租期信息表和支付类型表
     *
     * @param id 传入的租期id
     */
    @Override
    public AgreementVo getAgreementById(Long id) {
        //思路：公寓信息、房间信息、支付类型和租期信息，可以通过租期信息表恶对应id进行获取
        //0. 先查询租期信息
        LeaseAgreement leaseAgreement = leaseAgreementMapper.selectById(id);

        //1.查询公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(leaseAgreement.getApartmentId());


        //2.查询房间信息
        RoomInfo roomInfo = roomInfoMapper.selectById(leaseAgreement.getRoomId());

        //3.查询支付类型
        PaymentType paymentType = paymentTypeMapper.selectById(leaseAgreement.getPaymentTypeId());


        //4.查询租期信息
        LeaseTerm leaseTerm = leaseTermMapper.selectById(leaseAgreement.getLeaseTermId());

        AgreementVo agreementVo = new AgreementVo();
        //将对象的属性值(源对象)拷贝到目标对象的对应属性，前提是属性名和类型一致
        BeanUtils.copyProperties(leaseAgreement,agreementVo);
        //此处为agreementVo特有的属性，需要单独设置
        agreementVo.setApartmentInfo(apartmentInfo);
        agreementVo.setRoomInfo(roomInfo);
        agreementVo.setPaymentType(paymentType);
        agreementVo.setLeaseTerm(leaseTerm);

        return agreementVo;
    }
}




